<template>
	<view class="container">
		<u-empty mode="data" text="暂无数据" icon="http://cdn.uviewui.com/uview/empty/data.png" v-if="AiList.length==0">
		</u-empty>
		<view v-else>
			<view v-for="(item,index) in AiList" :key="index" class="ai-list" @click="handleClick(item)">
				<view style="display: flex;align-items: center;">
					<img :src="item.avatarUrl" alt="" srcset="" />
					<view>
						<view class="title">{{item.name}}</view>
						<view class="desc">{{item.welcomeMsg}}</view>
					</view>
				</view>
				<u-icon name="arrow-right"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		GetAiDatasetListApi,
		GetUserInfoApi,
	} from '../../api/main'
	export default {
		data() {
			return {
				AiList: [],
				access_token: "",
				token_type: "",
			}

		},
		onLoad(options) {
			console.log("-----------")
			console.log(options)
			console.log("-----------")
			this.access_token = options.access_token
			this.token_type = options.token_type
			uni.setStorageSync("access_token", options.access_token)
			uni.setStorageSync("token_type", options.token_type)
		},
		onShow: async function(options) {
			
			await this.getUserInfo()
			this.getData()
		},
		methods: {
			handleClick(item) {
				uni.navigateTo({
					url: `/pages/message/index?conversationId=${item.conversationId}&datasetId=${item.datasetId}&title=${item.name}&access_token=${this.access_token}&avatar_url=${item.avatarUrl}&id=${item.id}`
				})
			},
			getUserInfo() {
				return new Promise((resolve,reject)=>{
						GetUserInfoApi().then(response => {
							uni.setStorageSync("userInfo", response.data.sysUser)
							return resolve()
						})
				})
				
			},
			getData() {
				GetAiDatasetListApi().then(response => {
					response.data.forEach((item, index) => {
						item.avatarUrl = `http://oai.huat.edu.cn:6302${item.avatarUrl}`
						// item.conversationId =
						// 	`chat-message-list-${item.id}-${uni.getStorageSync("userInfo").userId}-${this.access_token}`
						item.conversationId =
							`${item.footer}`
						item.datasetId = item.id
					})
					this.AiList = response.data
					uni.setStorageSync("AiList", this.AiList)
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		padding: 20rpx;

		.ai-list {
			display: flex;
			align-items: center;
			justify-content: space-between;
			background: #F0F8FF;
			margin-bottom: 20px;
			padding: 20rpx;
			border-radius: 10rpx;

			img {
				width: 60px;
				margin-right: 10px;
			}

			.title {
				font-weight: bold;
				margin-bottom: 10rpx;
			}

			.desc {
				color: #999;
				font-size: 12px;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				overflow: hidden;
				text-overflow: ellipsis;
			}
		}
	}
</style>